<?php
/**
 * Created by PhpStorm.
 * User: wanglong
 * Date: 2017/11/11
 * Time: 20:10
 * 使用pdo进行查询操作
 */
try{
    $pdo=new PDO("mysql:dbname=test;host=localhost",'root','');
    //var_dump($pdo);
    //设置错误处理模式为异常抛出
    $pdo->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_EXCEPTION);

    //准备SQL语句
    $sql="select name,phone,email from contentinfo where departmentid='D01'";

    try{
        //执行select查询 并返回PDOstatement对象
        $pdostatement=$pdo->query($sql);
        echo "一共从表中获取到".$pdostatement->rowCount()."条记录:\n";

        foreach ($pdostatement as $row){
            echo $row['name']."\t";
            echo $row['phone']."\t";
            echo $row['email']."\n";
        }
    }catch(PDOException $e){
        echo $e->getMessage();
    }
}catch(PDOException $e){
    echo '数据库连接失败'.$e->getMessage();
    exit;
}